#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

int main()
{
    pid_t id = fork();
    if (id < 0)
    {
        perror("fork");
        exit(1);
    }
    else if (id == 0)
    {
        printf("I am child: %d\n", getpid());
        // int* p = NULL;
        // *p = 0;
        //int a = 1, b = 0;
        //a = a / b;
        sleep(5);
        exit(5);
    }

    /*
    int status = 0;
    pid_t ret = wait(&status);
    if (ret > 0 && (status & 0xff) == 0) {
        printf("wait succeed, exit code: %d\n", (status >> 8) & 0xff);
    }
    else{
        printf("wait failed, sign code: %d\n", status & 0x7f);
    }
    */
    int status = 0;
    pid_t ret;
    while ((ret = waitpid(id, &status, WNOHANG)) == 0) {
        printf("child is running\n");
        sleep(1);
    }
    if (ret > 0 && (status & 0xff) == 0) {
        printf("wait succeed, exit code: %d\n", (status >> 8) & 0xff);
    }
    else if (ret > 0) {
        printf("core dump: %d\n", status >> 8 & 1);
        printf("wait failed, sign code: %d\n", status & 0x7f);
    }
    return 0;
}